
const tokens = {
  admin: { token: 'mock-admin-token' },
  editor: { token: 'mock-editor-token' },
  monitor: { token: 'mock-monitor-token' }
}

const userInfos = {
  'mock-admin-token': {
    id: 1,
    roles: ['admin', 'editor', 'monitor'],
    name: '超级管理员'
  },
  'mock-editor-token': {
    id: 2,
    roles: ['editor'],
    name: '配置员'
  },
  'mock-monitor-token': {
    id: 3,
    roles: ['monitor'],
    name: '监控员'
  }
}

const userPermission = {
  'mock-admin-token': [
    { name: 't1',
      children: {
        name: 'l1'
      }
    },
    { name: 't2',
      children: { }
    }
  ],
  'mock-editor-token': [
  ],
  'mock-monitor-token': [
  ]
}

module.exports = util => {
  var mdl = require('.')
  var getPath = p => mdl.getPath(p, 'user/')
  var api = {}

  api[getPath('loginByUserName')] = (req, res, next) => {
    const username = req.body.username
    const token = tokens[username]
    if (!token) {
      return mdl.respFailed(res, 'Account or password incorrect.')
    }
    mdl.resp(res, token)
  }

  api[getPath('getUserInfo')] = (req, res, next) => {
    const token = req.headers['x-token']
    const info = userInfos[token]
    if (!info) {
      return mdl.respFailed(res, 'User has no info.')
    }
    mdl.resp(res, info)
  }

  api[getPath('getPermission')] = (req, res, next) => {
    const token = req.headers['x-token']
    const p = userPermission[token]
    if (!p) {
      return mdl.respFailed(res, 'User has no permission.')
    }
    mdl.resp(res, p)
  }

  api[getPath('logout')] = {
  }

  return api
}

